package com.dengcj.epl.service.util;

import cn.hutool.db.Entity;
import com.dengcj.epl.dao.DB;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dengchunjie
 * @Description: 下拉框工具类
 * @date 2022/1/11 17:44
 */
public class SelectBoxUtil {
    /**
     * 根据表名字段名称和下拉选择框值获取下拉选择框描述(仅限主表使用)
     * @param tablename
     * @param columnName
     * @param selectValue
     * @return
     */
    public static String getSelectName(String tablename, String columnName,String selectValue){
        DB db = new DB();

        String sql = "select c.selectname from workflow_billfield a " +
                "inner join workflow_bill b on b.id=a.billid " +
                "inner join workflow_selectitem c on c.FIELDID=a.id " +
                "where b.tablename=? and a.fieldname=? and viewtype=0 and selectvalue=?";
        Entity entity = db.queryOne(sql, tablename, columnName, selectValue);
        return entity.getStr("selectname");
    }
    /**
     * 根据表名字段名称和下拉选择框值获取下拉选择框描述
     * @param tablename
     * @param columnName
     * @param selectValue
     * @return
     */
    public static String getSelectName(String tablename,int detailIndex, String columnName,String selectValue){
        DB db = new DB();

        String sql = "select c.selectname from workflow_billfield a " +
                "inner join workflow_bill b on b.id=a.billid " +
                "inner join workflow_selectitem c on c.FIELDID=a.id " +
                "where b.tablename=? and a.fieldname=? and viewtype=? and selectvalue=?";
        int viewtype = 0;
        //如果明细表下标大于-1则代表为明细表
        if(detailIndex > 0){
            viewtype = 1;
            sql += " and detailtable='"+tablename+"_dt"+detailIndex+"'";
        }
        Entity entity = db.queryOne(sql, tablename, columnName, viewtype, selectValue);
        return entity.getStr("selectname");
    }

    /**
     * 根据字段编号和下拉选择框值获取下拉选择框描述
     * @param fieldid
     * @param selectValue
     * @return
     */
    public static String getSelectName(int fieldid,String selectValue){
        DB db = new DB();
        Entity entity = db.queryOne("select selectname from workflow_selectitem where fieldid=? and selectvalue=?", fieldid, selectValue);

        return entity.getStr("selectname");
    }

    /**
     * 根据字段编号和下拉选择框描述获取下拉选择框值
     * @param fieldid
     * @param fieldname
     * @return
     */
    public static int getSelectIndex(int fieldid,String fieldname){
        DB db = new DB();
        Entity entity = db.queryOne("select selectvalue from workflow_selectitem where fieldid=? and selectname=?",fieldid,fieldname);
        return entity.getInt("selectvalue");
    }

    /**
     * 根据字段编号获取下拉选择框内容
     * @param fieldid
     * @return
     */
    public static List<Entity> getSelectInfo(int fieldid){
        DB db = new DB();
        List<String> selectinfoList = new ArrayList<>();
        List<Entity> entities = db.query("select selectname from workflow_selectitem where fieldid=? order by selectvalue", fieldid);
        return entities;
    }

    /**
     * 获取表单的下拉框字段信息
     * @param tablename
     * @return
     */
    public List<Entity> getSelectFieldInfo(String tablename){
        DB db = new DB();
        List<Map<String, Object>> allSelectInfo = new ArrayList<>();
        List<Entity> entities = db.query("select a.id,a.fieldname,d.labelname,GROUP_CONCAT(c.SELECTNAME) as selectnames,GROUP_CONCAT(c.SELECTVALUE) as selectindex from workflow_billfield a " +
                "inner join workflow_bill b on b.id=a.billid " +
                "inner join htmllabelinfo d on d.indexid=a.fieldlabel " +
                "right join workflow_selectitem c on c.FIELDID=a.id " +
                "where b.tablename=？ and a.fieldhtmltype=5 and d.languageid=7 GROUP by a.id,a.fieldname,d.labelname", tablename);

        return entities;
    }

    /**
     * 获取表单的下拉框字段信息
     * @param tablename
     * @return
     */
    public Entity getSelectFieldInfo(String tablename,String fieldname){
        DB db = new DB();
        Map<String, Object> fieldMap = null;
        Entity entity = db.queryOne("select c.SELECTNAME,c.SELECTVALUE from workflow_billfield a " +
                "inner join workflow_bill b on b.id=a.billid " +
                "inner join htmllabelinfo d on d.indexid=a.fieldlabel " +
                "right join workflow_selectitem c on c.FIELDID=a.id " +
                "where b.tablename=? and a.fieldhtmltype=5 and d.languageid=7 and a.fieldname=?", tablename,fieldname);

        return entity;
    }

}
